﻿package com.dcliment.utils
{
	import com.dcliment.models.IAssetPreload;
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.CSSLoader;
	import com.greensock.loading.ImageLoader;
	import com.greensock.loading.LoaderMax;
	import com.greensock.loading.SWFLoader;
	import com.greensock.loading.XMLLoader;
	import com.greensock.loading.display.ContentDisplay;
	
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.Security;
	import flash.system.SecurityDomain;
	
	/**
	 * Will preload assets for you ::
	 * 
	 * NOTE :: add ability to look "type" the content is. so it will push back swf or whatever back at you.
	 *  
	 * @author dcliment
	 * version 1.1
	 */	

	public class AssetPreloader extends EventDispatcher
	{
		
		public static const PRELOAD_COMPLETE:String = "PRELOAD_COMPLETE";
		public static const PRELOAD_ERROR:String = "PRELOAD_ERROR";
		
		protected var _preloadQueue:LoaderMax;
		
		
		public function AssetPreloader()
		{
			_preloadQueue = new LoaderMax({name:"mainqueue", onProgress:preloadProgressHandler, onComplete:preloadCompleteHandler, onError:preloadErrorHandler, onChildComplete:imageCompleteHandler});
		}
		
		/**
		 * This uses Greensock's built in xml reader to create a queue
		 *  
		 * @param assetList
		 * 
		 */		
		public function addXMLToQueu(assetList:Array):void
		{
			
			for(var x:uint=0; x<assetList.length;x++){
				
				var assetLoaderObj:Object = assetList[x];
				
				switch(assetLoaderObj.type){
					
					
					case "XML":
						_preloadQueue.append( new XMLLoader(assetLoaderObj.path, {name:assetLoaderObj.name}) );
					break;
					
					case "CSS":
						
						_preloadQueue.append( new CSSLoader(assetLoaderObj.path,{name:assetLoaderObj.name}));
						
						break;
					
					case "SWF":
						_preloadQueue.append( new SWFLoader(assetLoaderObj.path,{name:assetLoaderObj.name, context:assetLoaderObj.ldrContext}));
						
						break;
					
					case "IMG":
						
						_preloadQueue.append( new ImageLoader(assetLoaderObj.path,{name:assetLoaderObj.name}));
						
						break;
					
				}
			}
				
		}
		/**
		 * $model must contain
		 * $model.type = "SWF" or "IMG" or "XML";
		 * $model.path = "path to asset";
		 * $model.name = "name used to get the asset later";
		 *  
		 * @param $model
		 * 
		 */		
		public function addToQueue($model:IAssetPreload):void
		{
			
			var type:String = $model.type.toUpperCase();
			
			switch(type)
			{
				case "SWF":
					var appDomain:ApplicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain); // OK.... applicationDomain has to be currDomain for you to cast as IContent
					var cxt:LoaderContext = new LoaderContext(false, appDomain);
					
					if(Security.sandboxType == Security.REMOTE)
					{
						cxt.securityDomain = SecurityDomain.currentDomain; // this is as a failsafe if loaded from server
					}
					
					_preloadQueue.append(new SWFLoader($model.path, {name:$model.name, context:cxt}));
					
					break;
				case "IMG":
				
					_preloadQueue.append( new ImageLoader($model.path,{name:$model.name}));
					
					break;
				
				case "XML":
					
					_preloadQueue.append( new XMLLoader($model.path, {name:$model.name}) );
					
					break;
				
				default:
					
					throw new Error("AssetPreloader.addToQueue() ERROR :: $model.type = " + $model.type + " :: DOES NOT MATCH PRESETS");
					break;
					
			}

		}
		
		/**
		 * START IT 
		 * 
		 */		
		public function startQueue():void
		{
			_preloadQueue.load();
		}
		// ------------------------
		// GET resources
		public function getAssetByName(name:String):ContentDisplay
		{ 
			return LoaderMax.getContent(name);
		}
		
		public function getImageByName(name:String):Bitmap
		{
			var imgContent:ContentDisplay = LoaderMax.getContent(name);
			return imgContent.rawContent;
		}
		
		public function getContentArray():*
		{
			trace(_preloadQueue.content);
		}
		//------------------------
		// EVENTS
		protected function preloadCompleteHandler(event:LoaderEvent):void {
			
			
			dispatchEvent(new Event(AssetPreloader.PRELOAD_COMPLETE));
			trace("AssetPreloader :: PreloadComplete");
		}
		
		
		protected function preloadProgressHandler(event:LoaderEvent):void 
		{
			
			//trace("progress: " + event.target.progress);
			
		}
		
		
		protected function preloadErrorHandler(event:LoaderEvent):void {
			
			dispatchEvent(new Event(AssetPreloader.PRELOAD_ERROR));
			
		}
		protected function imageCompleteHandler(event:LoaderEvent):void
		{
			/*
			trace("AssetPreloader.imageCompleteHandler :: event: \t" + event);
			trace("AssetPreloader.imageCompleteHandler :: event.target: \t" + event.target);
			trace("AssetPreloader.imageCompleteHandler :: event.target.content: \t" + event.target.content);
			trace("\n");
			*/
			
		}
	}
}